import { dayjs } from 'element-plus'
import { isObject } from 'lodash-es'

/**
 * @function   格式化时间日期指令
 * @example    <span v-date="{date: '2022-12-08', format: 'YYYY-MM HH:mm'}"></span>
 */

export default {
	mounted(el: Element, binding: any) {
		formatDate(el, binding)
	},
	updated(el: Element, binding: any) {
		formatDate(el, binding)
	},
}

function formatDate(el: Element, binding: any) {
	if (!isObject(binding.value)) return ''
	const { date = '', format = 'YYYY-MM-DD HH:mm:ss' } = binding.value
	el.innerHTML = !date ? '' : dayjs(date).format(format)
}
